// 订单相关的API

import request from '@/utils/request'

/**
 * 生成订单(即填写订单或结算页)
 * 根据购物车中的商品生成订单
 * @returns {*|AxiosPromise}
 */
export const createOrder = () => {
  return request('/member/order/pre', 'get')
}

/**
 * 生成订单(即填写订单或结算页)
 * 根据订单中的商品生成订单（再次购买—）
 * @returns {*|AxiosPromise}
 */
export const repurchaseOrder = (id) => {
  return request(`/member/order/repurchase/${id}`, 'get')
}

/**
 * 添加收货地址
 * @param form {Object}
 * @returns {*|AxiosPromise}
 */
export const addAddress = (form) => {
  return request('/member/address', 'post', form)
}

/**
 * 更新收货地址
 * @param form {Object}
 * @returns {*|AxiosPromise}
 */
export const editAddress = (form) => {
  return request(`/member/address/${form.id}`, 'put', form)
}

/**
 * 提交订单
 * @param params
 * @returns {*|AxiosPromise}
 */
export const submitOrder = (params) => {
  return request('/member/order', 'post', params)
}

/**
 * 获取订单详情
 * @param orderId {String} 订单id
 * @returns {*|AxiosPromise}
 */
export const findOrderDetail = (orderId) => {
  return request(`/member/order/${orderId}`, 'get')
}

/**
 * 获取我的订单
 * @param page 页码
 * @param pageSize 每页条数
 * @param orderState 订单状态，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消，未传该参数或0为全部
 * @returns {*|AxiosPromise}
 */
export const findOrderList = ({
  page = 1,
  pageSize = 10,
  orderState = 1
}) => {
  return request('/member/order', 'get', {
    page,
    pageSize,
    orderState
  })
}

/**
 * 取消订单
 * @param id {String} 订单id
 * @param cancelReason {String} 取消原因
 * @returns {*|AxiosPromise}
 */
export const cancelOrder = ({
  id,
  cancelReason
}) => {
  return request(`/member/order/${id}/cancel`, 'put', { cancelReason })
}

/**
 * 删除订单
 * @param ids {Array<string>} 订单id集合
 * @returns {*|AxiosPromise}
 */
export const deleteOrder = (ids) => {
  return request('/member/order', 'DELETE', { ids })
}

/**
 * 确认收货
 * @param id{String} 订单id
 * @returns {*|AxiosPromise}
 */
export const confirmOrder = (id) => {
  return request(`/member/order/${id}/receipt`, 'PUT')
}

/**
 * 查看物流
 * @param id{String}
 * @returns {*|AxiosPromise}
 */
export const logisticsOrder = (id) => {
  return request(`/member/order/${id}/logistics`, 'get')
}
